﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.ServiceModel.DomainServices.Client;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using FinalSilverlightProject.Web;
using FinalSilverlightProject.Web.Services;


namespace FinalSilverlightProject.Views.Login
{
    public partial class ConfirmDeleteAccountWindow : ChildWindow
    {
        private readonly UserRegistrationContext _editUserAcountContext = new UserRegistrationContext();
        public ConfirmDeleteAccountWindow()
        {
            InitializeComponent();
        }

        private void OkButtonClick(object sender, RoutedEventArgs e)
        {
            string currentUser = WebContext.Current.User.Name;
            WebContext.Current.Authentication.Logout(logoutOperation =>
            {
                if (logoutOperation.HasError)
                {
                    ErrorWindow.CreateNew(logoutOperation.Error);
                    logoutOperation.MarkErrorAsHandled();
                }
                else
                {
                    this.busyIndicator.IsBusy = true;
                    InvokeOperation op = _editUserAcountContext.DeleteUser(currentUser);
                    op.Completed += (s, ex) =>
                    {
                        this.busyIndicator.IsBusy = false;
                        if (!op.HasError)
                        {
                            this.DialogResult = true;
                            return;
                        }
                    };

                }
            }, /* userState */ null);
        }

        private void CancelButtonClick(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }
    }
}

